<?php
class ControllerSaleDelivery extends Controller {
	private $error = array();
	
	public function index() { 
		$this->load->language('sale/delivery');
        $this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/delivery'); 
		
		$this->getList();
	}
	
	public function insert() {
		$this->load->language('sale/delivery');
        $this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/delivery');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_sale_delivery->addDelivery($this->request->post);			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = $this->getFilters();
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

            $this->redirect($this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('sale/delivery');
        $this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/delivery');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_sale_delivery->editDelivery($this->request->get['delivery_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

            $url = $this->getFilters();
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

            $this->redirect($this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() { 
		$this->load->language('sale/delivery');
        $this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/delivery');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
      		foreach ($this->request->post['selected'] as $delivery_id) {
				$this->model_sale_delivery->deleteDelivery($delivery_id);	
			}
						
			$this->session->data['success'] = $this->language->get('text_success');

            $url = $this->getFilters();
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}
			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

            $this->redirect($this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL'));
		}

		$this->getList();
	}
	
	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'delivery_id';
		}
		 
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

        $url = $this->getFilters();
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

        $this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('common/home', 'token='.$this->session->data['token'], 'SSL'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => false
   		);
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL'),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('sale/delivery/insert', 'token='.$this->session->data['token'].$url, 'SSL');
		$this->data['delete'] = $this->url->link('sale/delivery/delete', 'token='.$this->session->data['token'].$url, 'SSL');
	
		$this->data['delivery_list'] = array();
		$data = array(
            'filter_city'   => $this->getFilterData('filter_city'),
            'filter_price'	=> $this->getFilterData('filter_price'),
			'sort'          => $sort,
			'order'         => $order,
			'start'         => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'         => $this->config->get('config_admin_limit')
		);
		
		$delivery_list_total = $this->model_sale_delivery->getTotalDeliveries($data);
		$results = $this->model_sale_delivery->getDeliveries($data);
		foreach ($results as $result) {
			$action = array();			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('sale/delivery/update', 'token='.$this->session->data['token'].'&delivery_id='.$result['delivery_id'].$url, 'SSL'),
			);		
		
			$this->data['delivery_list'][] = array(
				'delivery_id' 	=> $result['delivery_id'],
				'postcode'      => $result['postcode_default'],
				'city'          => $result['city'],
				'price'        	=> $result['price'],
				'action'        => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_id'] = $this->language->get('column_id');
		$this->data['column_postcode'] = $this->language->get('column_postcode');
		$this->data['column_city'] = $this->language->get('column_city');
		$this->data['column_price'] = $this->language->get('column_price');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
        $this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

        $url = $this->getFilters();
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		$this->data['sort_id'] = $this->url->link('sale/delivery', 'token='.$this->session->data['token'].'&sort=delivery_id'.$url, 'SSL');
		$this->data['sort_city'] = $this->url->link('sale/delivery', 'token='.$this->session->data['token'].'&sort=city'.$url, 'SSL');
		$this->data['sort_price'] = $this->url->link('sale/delivery', 'token='.$this->session->data['token'].'&sort=price'.$url, 'SSL');

        $this->data['filter_city'] = $this->getFilterData('filter_city');
        $this->data['filter_price'] = $this->getFilterData('filter_price');
        $this->data['filter_url'] = $this->url->link('sale/delivery'); //, 'token='.$this->session->data['token'], 'SSL');
        $this->data['token'] = $this->session->data['token'];

        $url = $this->getFilters();
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $delivery_list_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url.'&page={page}', 'SSL');
		
		$this->data['pagination'] = $pagination->render();
		$this->data['sort'] = $sort; 
		$this->data['order'] = $order;
		
		$this->template = 'sale/delivery_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());
 	}
	
	private function getForm() {
		$this->data['heading_title'] = $this->language->get('entry_title');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_id'] = $this->language->get('entry_id');
		$this->data['entry_postocde'] = $this->language->get('entry_postocde');
		$this->data['entry_city'] = $this->language->get('entry_city');
		$this->data['entry_price'] = $this->language->get('entry_price');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['city'])) {
			$this->data['error_city'] = $this->error['city'];
		} else {
			$this->data['error_city'] = '';
		}
		if (isset($this->error['price'])) {
			$this->data['error_price'] = $this->error['price'];
		} else {
			$this->data['error_price'] = '';
		}

		$url = $this->getFilters();
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

        $this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('common/home', 'token='.$this->session->data['token'], 'SSL'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);
        $this->data['breadcrumbs'][] = array(
       		'href'      => $this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL'),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
			
		if (!isset($this->request->get['delivery_id'])) {
			$this->data['action'] = $this->url->link('sale/delivery/insert', 'token='.$this->session->data['token'].$url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('sale/delivery/update', 'token='.$this->session->data['token'].'&delivery_id='.$this->request->get['delivery_id'].$url, 'SSL');
		}		  
    	$this->data['cancel'] = $this->url->link('sale/delivery', 'token='.$this->session->data['token'].$url, 'SSL');

		if (isset($this->request->get['delivery_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$delivery = $this->model_sale_delivery->getDelivery($this->request->get['delivery_id']);
		}

		if (isset($this->request->post['postcode'])) {
			$this->data['postcode'] = $this->request->post['postcode'];
		} elseif (isset($delivery)) {
			$this->data['postcode'] = $delivery['postcode_default'];
		} else {
			$this->data['postcode'] = '';
		}
		if (isset($this->request->post['city'])) {
			$this->data['city'] = $this->request->post['city'];
		} elseif (isset($delivery)) {
			$this->data['city'] = $delivery['city'];
		} else {
			$this->data['city'] = '';
		}
		if (isset($this->request->post['price'])) {
			$this->data['price'] = $this->request->post['price'];
		} elseif (isset($delivery)) {
			$this->data['price'] = $delivery['price'];
		} else {
			$this->data['price'] = '';
		}
			
		$this->template = 'sale/delivery_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'sale/delivery')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ((strlen(utf8_decode($this->request->post['city'])) < 0) || (strlen(utf8_decode($this->request->post['city'])) > 255)) {
			$this->error['city'] = $this->language->get('error_city');
		}
		if ((strlen(utf8_decode($this->request->post['price'])) < 0) || (strlen(utf8_decode($this->request->post['price'])) > 255)) {
			$this->error['price'] = $this->language->get('error_price');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'sale/delivery')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

    private function getFilters(){
        $url = '';
        if(isset($this->request->get['filter_city'])){
            $url .= '&filter_city=' . urlencode(html_entity_decode($this->request->get['filter_city'], ENT_QUOTES, 'UTF-8'));
        }
        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_price=' . $this->request->get['filter_price'];
        }

        return $url;
    }

    private function getFilterData($field){
        return isset($this->request->get[$field]) ? $this->request->get[$field] : null;
    }
}
?>